#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QAudioDeviceInfo>
#include <QAudioInput>
#include <QAudioOutput>
#include <QUdpSocket>
#include <QAudioRecorder>
#include <QAction>
#include <QAudioEncoderSettings>
#include <QTimer>
#include <QNetworkInterface>
#include <QSettings>
#include <QNetworkDatagram>
#include <QThread>
#include <QTcpSocket>
#include <QTcpServer>
#include <QMenu>
#include <QMessageBox>
#include <QGridLayout>
#include <QSystemTrayIcon>
#include "recvdata.h"
#include "callwaiting.h"
#include "protocolmanager.h"
#include "timerwidget.h"

#include <QFile>
#include <QDebug>

// WAV格式头
struct WAVHEADER
{
    // RIFF 头
    char RiffName[4];
    unsigned long nRiffLength;
    // 数据类型标识符
    char WavName[4];
    // 格式块中的块头
    char FmtName[4];
    unsigned long nFmtLength;
    // 格式块中的块数据
    unsigned short nAudioFormat;
    unsigned short nChannleNumber;
    unsigned long nSampleRate;
    unsigned long nBytesPerSecond;
    unsigned short nBytesPerSample;
    unsigned short nBitsPerSample;
    // 数据块中的块头
    char    DATANAME[4];
    unsigned long   nDataLength;
};

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

signals:
    void signal_hangUp();

public Q_SLOTS:
    void handleStateChanged(QAudio::State state);

    void slot_callRequest();

    void slot_callResponse(int response);

    void slot_sendAudioData();

    void slot_callCancel();

    void slot_manualCancel();

    void slot_systemTrayActived(QSystemTrayIcon::ActivationReason reason);

private slots:
    void slot_reciveAudioDate();

    void slot_setRates(QAction *action);

    void slot_setSize(QAction *action);

    void slot_setChannel(QAction *action);

    void slot_setCodec(QAction *action);

    void slot_setByteOrder(QAction *action);

    void slot_setType(QAction *action);

    void on_toolBtnCall_clicked();

    void on_toolBtnHangUp_clicked();

    void slot_connected();

    void slot_disconnected();

    void on_hSliderVolume_valueChanged(int value);

private:
    void readConfig();

    void initToolButton();

    void initMessageBox();

    void initDialog();

    void initAudioInput();

    void initUDP();

    void initThread();

    void addSettingItem(const QString itemName, QStringList settingOption, void (MainWindow::*function) (QAction *));

    void initMenuBar();

    void initSystemTrayIcon();

    QHostAddress hostIP();

    void setSettingsDisabled(bool disable);

private:
    Ui::MainWindow *ui;
    QAudioFormat m_format;

    QAudioInput *m_audioInput;
    QIODevice *m_inputDevice;
    QFile sourceFile;

    QAudioOutput *m_audioOutput;
    QIODevice *m_outputDevice = nullptr;

    QString m_targetIP;
    int m_targetPort;
    int m_hostPort;
    QUdpSocket m_socket;

    QAudioRecorder *m_audioRecorder;

    RecvData *m_recvData;
    QThread *m_writeThread;

    QAction *m_settingAction;
    QMenu *m_settingMenu;

    // 用于将实际的枚举值转换成字符串，便于阅读
    QMap<int, QString> m_endien;
    QMap<int, QString> m_sampleType;

    QMessageBox m_messageBox;
    QDialog m_dialog;
    CallWaiting m_callWaiting;
    QTimer m_dialogTimer;

    ProtocolManager m_protocolManager;

    QSystemTrayIcon m_tray;
    QMenu m_trayMenu;
};
#endif // MAINWINDOW_H
